package com.jy.adscm.dealer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.DealerInfo;
import com.jy.adscm.dealer.mapper.DealerInfoMapper;
import com.jy.adscm.dealer.service.DealerInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-16
 */
@Service
public class DealerInfoServiceImpl extends ServiceImpl<DealerInfoMapper, DealerInfo> implements DealerInfoService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public List<DealerInfo> selectDealerInfo(Integer did) {
        Wrapper<DealerInfo> wrapper =new EntityWrapper<>();
        wrapper.eq("did",did);
        List<DealerInfo> dealerInfos = super.selectList(wrapper);
        for (DealerInfo dealerInfo : dealerInfos) {
      if (AdscmBaseUtils.getInstance().isNotEmpty(dealerInfo.getCreator(),null)){
          JyUser jyUser = jyUserMapper.selectById(dealerInfo.getCreator());
          dealerInfo.setCreatorName(jyUser.getName());
         }
        }
        return dealerInfos;
    }

    @Override
    public void insertDealerInfo(Object obj, Token token,Class cls,Integer did) {
        DealerInfo dealerInfo =new DealerInfo();
        dealerInfo.setDid(did);
        dealerInfo.setTime(new Date());
        dealerInfo.setCreator(token.getUser().getId().toString());
        String content="新增 "+AdscmBaseUtils.getInstance().getFieldsInfo(obj,cls);
        dealerInfo.setContent(content);
        super.insert(dealerInfo);
    }

    @Override
    public void updateDealerInfo(Object newobj, Object oldobj, Token token ,Class cls,Integer did) throws AdscmSystemException {
        DealerInfo dealerInfo =new DealerInfo();
        dealerInfo.setDid(did);
        dealerInfo.setTime(new Date());
        dealerInfo.setCreator(token.getUser().getId().toString());
        String content="修改 "+AdscmBaseUtils.getInstance().compareObj(newobj,oldobj,cls);
        dealerInfo.setContent(content);
        super.insert(dealerInfo);
    }

    @Override
    public void deleteDealerInfo(Object obj, Token token,Class cls,Integer did) {
        DealerInfo dealerInfo =new DealerInfo();
        dealerInfo.setDid(did);
        dealerInfo.setTime(new Date());
        dealerInfo.setCreator(token.getUser().getId().toString());
        String content="删除 "+AdscmBaseUtils.getInstance().getFieldsInfo(obj,cls);
        dealerInfo.setContent(content);
        super.insert(dealerInfo);

    }
}
